package org.mobile.servlets;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.mobile.beans.User;
import org.mobile.logic.UserLogic;

/**
 * Servlet implementation class AddUser
 */
@WebServlet(name = "addUser", urlPatterns = { "/addUser" })
public class AddUser extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddUser() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		User user= new User();
		String password= request.getParameter("password");
		String retypedPass= request.getParameter("repeatpass");
		String name=  new String(request.getParameter("name").getBytes("ISO8859_1"), "UTF-8");
		String surname= new String(request.getParameter("surname").getBytes("ISO8859_1"), "UTF-8");
		String address= new String(request.getParameter("address").getBytes("ISO8859_1"), "UTF-8");
		String login= request.getParameter("login");
		if(!login.isEmpty() && !password.isEmpty() && !surname.isEmpty() && !name.isEmpty() && !address.isEmpty()) {
			if(password.equals(retypedPass)){
				try {
					BeanUtils.populate(user, request.getParameterMap());
					user.setStyle(User.DEFAULT_THEME);
					user.setName(name);
					user.setSurname(surname);
					user.setAddress(address);
					if(!UserLogic.getInstance().addUser(user)){
						request.setAttribute("error", "Błąd dodawania użytkownika!");
						request.getRequestDispatcher("errPage.jsp").forward(request, response);
					}
					else{
						request.getSession().setAttribute("users", UserLogic.getInstance().getUsers());
						request.setAttribute("message", "Użtkownik został dodany");		
						request.getRequestDispatcher("index.jsp").forward(request, response);
					}
					
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
			else{
				request.setAttribute("error", "Podane hasła nie zgadzają się!");
				request.getRequestDispatcher("errPage.jsp").forward(request, response);
			}
		}
		else {
			request.setAttribute("error", "Błąd dodawania użytkownika, wypełnij wszystkie pola!");
			request.getRequestDispatcher("errPage.jsp").forward(request, response);
		}
	}

}
